package Leetcode第一期;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @author : K k
 * @date : 9:53 2020/5/14
 */
public class 合并区间_56 {
    public int[][] merge(int[][] intervals) {
        List<int[]> list=new ArrayList<>();
        if (intervals.length==0||intervals==null)return list.toArray(new int[0][]);
        //升序排序
        Arrays.sort(intervals, (a, b) -> a[0] - b[0]);

        for (int i=0;i<intervals.length;i++){
            int left=intervals[i][0];
            int right=intervals[i][1];
            if (i<intervals.length-1&&right>=intervals[i+1][0]){
                //合并
                right= Math.max(right,intervals[i+1][1]);
            }
            list.add(new int[]{left,right});
        }
        return list.toArray(new int[0][]);
    }
}
